<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="app-nav-bar">
      <van-button
        class="search-btn"
        slot="title"
        icon="search"
        type="info"
        round
        size="small"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->
    <!-- 文章频道 -->
    <van-tabs class="channel-tabs" v-model="active">
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <article-list :channel="channel"></article-list>
      </van-tab>

      <div slot="nav-right" class="wap-nav-placeholder"></div>

      <div slot="nav-right" @click="isChannelEditShow = true">
        <van-icon name="wap-nav" class="wap-nav-wrap" />
      </div>
    </van-tabs>
    <!-- 、文章频道 -->

    <van-popup
      class="channel-edit-popup"
      v-model="isChannelEditShow"
      position="bottom"
      closeable
      close-icon-position="top-left"
      get-container="body"
      style="height: 100%"
    >
      <channel-edit
        :user-channels="channels"
        :active="active"
        @close="isChannelEditShow = false"
        @update-active="active = $event"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from "@/API/user";
import ArticleList from "../../components/article-list.vue";
import ChannelEdit from "../../components/channel-edit.vue";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage";
export default {
  name: "HomeIndex",
  components: {
    ArticleList,
    ChannelEdit,
  },
  computed: {
    ...mapState(["user"]),
  },
  data() {
    return {
      active: 0, //控制被激活的标签
      channels: [], //频道列表
      isChannelEditShow: false, //控制编辑频道的显示状态
    };
  },
  created() {
    this.loadChannels();
  },
  methods: {
    //请求用户频道数据
    async loadChannels() {
      let channels = [];
      if (this.user) {
        //登陆了
        const { data } = await getUserChannels();
        channels = data.data.channels;
      } else {
        //没登陆
        const localChannels = getItem("user-channels");
        if (localChannels) {
          channels = localChannels
        }else{
          const { data } = await getUserChannels();
          channels = data.data.channels;
        }
      }
      this.channels = channels
    },
    // onUpdateActive(index){
    //   this.active = index;
    // }
  },
};
</script>

<style lang="less" scoped>
.home-container {
  /deep/ .van-nav-bar__title {
    max-width: none;
  }
  .search-btn {
    width: 277px;
    height: 32px;
    background: #5babfb;
    border: none;
    .van-icon {
      font-size: 16px;
    }
    .van-button__text {
      font-size: 14px;
    }
  }
  .channel-tabs {
    /deep/ .van-tab {
      border-right: 1px solid #edeff3;
      border-bottom: 1px solid #edeff3;
    }
    /deep/ .van-tabs__line {
      bottom: 20px;
      width: 15px !important;
      height: 3px;
      background-color: #3296fa;
    }
  }
  .wap-nav-placeholder {
    width: 33px;
    flex-shrink: 0;
    position: fixed;
    right: 0;
  }
  .wap-nav-wrap {
    position: fixed;
    right: 0;
    height: 44px;
    width: 33px;
    line-height: 44px;
    background-color: #fff;
    opacity: 0.9;
    display: flex;
    justify-content: center;
    align-items: center;
    .van-icon {
      font-size: 24px;
    }
  }
}
</style>